package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookReviewService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;

/**
 * 评论接口
 * @author xusaike
 *
 */
@Component
@Path("/v{version}/review")
public class BookReviewRestController {
	
	@Resource
	private BookReviewService bookReviewService;
	
	/**
	 * 获取评论书籍列表
	 * @param book_id
	 * @param pages
	 * @param contents
	 * @param order
	 * @return
	 */
	@GET
    @Path("/book/list")
	public String getBook(@QueryParam("book_id")int book_id,@QueryParam("pages")int pages,@QueryParam("contents")int contents,
			@QueryParam("order")String order){
		try {
			order=IReadUtils.preventInjection(order);
			String ret = bookReviewService.getReviewList(0,book_id,0,pages,contents,order,0);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 获取评论书籍列表
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/book/essence/list")
	public String getBookEssence(@QueryParam("book_id")int book_id,@QueryParam("pages")int pages,@QueryParam("contents")int contents,@QueryParam("order")String order){
		try {
			order=IReadUtils.preventInjection(order);
			String ret = bookReviewService.getReviewList(0,book_id,1,pages,contents,order,0);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 获取章节评论
	 * @param book_id
	 * @param pages
	 * @param contents
	 * @param order
	 * @return
	 */
	@GET
    @Path("/chapter/list")
	public String getChapters(@QueryParam("book_id")int book_id,@QueryParam("chapter_id")int chapter_id,@QueryParam("pages")int pages,@QueryParam("contents")int contents,@QueryParam("order")String order){
		try {
			order=IReadUtils.preventInjection(order);
			String ret = bookReviewService.getReviewList(1,book_id,0,pages,contents,order,chapter_id);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "获取章节列表",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 评论章节
	 * @param request
	 * @param book_id
	 * @param chap_id
	 * @param title
	 * @param content
	 * @param type
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/add")
	public String addReview(@Context HttpServletRequest request,
			@FormParam("book_id")int book_id,
			@FormParam("chapter_id")int chap_id,
			@FormParam("title")String title,
			@FormParam("content")String content,
			@FormParam("type")int type,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ip = request.getRemoteAddr();
			String ret = bookReviewService.addReview(ip, book_id, chap_id, title, content, type,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
    @Path("/delete")
	public String deleteReview(
			@FormParam("review_id")int review_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookReviewService.deleteReview(review_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 设置置顶
	 * @param request
	 * @param book_id
	 * @param chap_id
	 * @param title
	 * @param content
	 * @param type
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/top")
	public String updateTop(
			@FormParam("review_id")int review_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookReviewService.updateTop(review_id,token,device_identifier,1);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 加精
	 * @param review_id
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
	@Path("/essence")
	public String updateEssence(
			@FormParam("review_id")int review_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
			){
		try {
			String ret = bookReviewService.updateEssence(review_id,token,device_identifier,1);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 取消置顶
	 * @param request
	 * @param book_id
	 * @param chap_id
	 * @param title
	 * @param content
	 * @param type
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/top/delete")
	public String deleteTop(
			@FormParam("review_id")int review_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookReviewService.updateTop(review_id,token,device_identifier,0);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 取消加精
	 * @param request
	 * @param book_id
	 * @param chap_id
	 * @param title
	 * @param content
	 * @param type
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
	@Path("/essence/delete")
	public String deleteEssence(
			@FormParam("review_id")int review_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
			){
		try {
			String ret = bookReviewService.updateEssence(review_id,token,device_identifier,0);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	
	/**
	 * 回复评论
	 * @param request
	 * @param book_id
	 * @param chap_id
	 * @param title
	 * @param content
	 * @param type
	 * @param token
	 * @param device_identifier
	 * @return
	 */
	@POST
    @Path("/reply/add")
	public String addReply(@Context HttpServletRequest request,
			@FormParam("review_id")int review_id,
			@FormParam("content")String content,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ip = request.getRemoteAddr();
			String ret = bookReviewService.addReply(ip, review_id, content,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("review_id", review_id);
			errorJson.put("content", content);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
    @Path("/reply/delete")
	public String deleteReply(
			@FormParam("reply_id")int reply_id,
			@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier
		){
		try {
			String ret = bookReviewService.deleteReply(reply_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("reply_id", reply_id);
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "添加评论异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
}
